﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SaleSoft.Models;
using SaleSoft.Models.ScreenClient;
using Telerik.Web.Mvc;
using SaleSoft.Constants;
using SaleSoft.Helper;
using System.Data;

namespace SaleSoft.Areas.administrator.Controllers
{
    public class LoaiWebMauController : BaseController
    {
        #region Properties Section
        private SaleSoftEntities db = new SaleSoftEntities();
        #endregion

        #region Views Section
        public ViewResult Index () {
            LoaiWebScreenClient screen = new LoaiWebScreenClient();
            if (Session[ScreenClientConstants.LOAI_WEB] != null) {
                screen = (LoaiWebScreenClient)Session[ScreenClientConstants.LOAI_WEB];
            }
            Search(screen);
            return View(screen);
        }

        [GridAction]
        public ActionResult _Index ( LoaiWebScreenClient screen ) {
            Search(screen);
            return View(new GridModel(screen.DSLoai));
        }

        #endregion

        #region CRUD Section

        [HttpPost]
        [GridAction]
        public ActionResult _Insert () {
            SaleSoft.Models.BaseClient.loai_web loaiClient = new Models.BaseClient.loai_web();
            TryUpdateModel(loaiClient);

            loai_web loai = new loai_web() {
                tenloaiweb = loaiClient.tenloaiweb,
                sothutu = loaiClient.sothutu
            };

            db.loai_web.AddObject(loai);
            db.SaveChanges();

            LoaiWebScreenClient screen = new LoaiWebScreenClient();
            if (Session[ScreenClientConstants.LOAI_WEB] != null) {
                screen = (LoaiWebScreenClient)Session[ScreenClientConstants.LOAI_WEB];
            }
            Search(screen);
            return View(new GridModel(screen.DSLoai));
        }

        [HttpPost]
        [GridAction]
        public ActionResult _Edit ( int id ) {
            SaleSoft.Models.BaseClient.loai_web loaiClient = new Models.BaseClient.loai_web();
            TryUpdateModel(loaiClient);
            loaiClient.id_loaiweb = id;
            loai_web loai = db.loai_web.Single(p => p.id_loaiweb == id);
            ObjectHelper.Copy(loaiClient, loai);

            db.ObjectStateManager.ChangeObjectState(loai, EntityState.Modified);
            db.SaveChanges();
            LoaiWebScreenClient screen = new LoaiWebScreenClient();
            if (Session[ScreenClientConstants.LOAI_WEB] != null) {
                screen = (LoaiWebScreenClient)Session[ScreenClientConstants.LOAI_WEB];
            }
            Search(screen);
            return View(new GridModel(screen.DSLoai));
        }

        [HttpPost]
        [GridAction]
        public ActionResult _Delete ( int id ) {
            loai_web loai = db.loai_web.Single(p => p.id_loaiweb == id);
            if (loai.ds_webmau.Count == 0) {
                db.loai_web.DeleteObject(loai);
                db.SaveChanges();
            }
            LoaiWebScreenClient screen = new LoaiWebScreenClient();
            if (Session[ScreenClientConstants.LOAI_WEB] != null) {
                screen = (LoaiWebScreenClient)Session[ScreenClientConstants.LOAI_WEB];
            }
            Search(screen);
            return View(new GridModel(screen.DSLoai));
        }
        #endregion

        #region Helper Section
        private void Search ( LoaiWebScreenClient screen ) {
            var data = db.loai_web.OrderBy(o => o.tenloaiweb);
            screen.DSLoai.Clear();
            foreach (loai_web loai in data) {
                SaleSoft.Models.BaseClient.loai_web loaiClient = new Models.BaseClient.loai_web();
                ObjectHelper.Copy(loai, loaiClient);
                loaiClient.Deleteable = loai.ds_webmau.Count == 0;
                screen.DSLoai.Add(loaiClient);
            }
            Session[ScreenClientConstants.LOAI_WEB] = screen;
        }
        #endregion

        #region Additional Section
        protected override void Dispose ( bool disposing ) {
            db.Dispose();
            base.Dispose(disposing);
        }

        #endregion
    }
}
